package cn.sangedon.review.analysis.controller;

import cn.sangedon.review.analysis.schedule.ScheduleDispatch;
import cn.sangedon.review.analysis.service.StockStrategyService;
import cn.sangedon.review.common.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "交易策略")
@RestController
@RequestMapping("strategy")
public class StrategyController {
    @Autowired
    private StockStrategyService stockStrategyService;

    @Autowired
    private ScheduleDispatch scheduleDispatch;

    /**
     * 查询列表
     *
     * @return
     */
    @ApiOperation("查询策略列表")
    @GetMapping("list/{status}")
    public Result listStrategy(@PathVariable Integer status) {
        return Result.okBuilder().data(stockStrategyService.listStrategy(status)).build();
    }

    /**
     * 执行策略
     *
     * @param id 策略ID
     * @return
     */
    @ApiOperation("执行策略")
    @GetMapping("exec/{id}")
    public Result execStrategy(@PathVariable Long id) {
        stockStrategyService.execStrategy(id);
        return Result.ok();
    }

    /**
     * 定期刷新定时任务配置
     *
     * @return
     */
    @ApiOperation("刷新定时任务配置")
    @GetMapping("refresh/schedule")
    public Result refreshSchedule() {
        scheduleDispatch.refresh();
        return Result.ok();
    }

    /**
     * 验证某个策略某个交易日的成功率
     *
     * @param id
     * @param time
     * @return
     */
    @ApiOperation("验证某个策略某个交易日的成功率")
    @GetMapping("verify/{id}/{time}")
    public Result verify(@PathVariable Long id, @PathVariable String time) {
        try {
            return Result.okBuilder().data(stockStrategyService.verify(id, time)).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error();
    }

    /**
     * 计算策略历史成功率
     *
     * @param id 策略ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    @ApiOperation("计算策略历史成功率")
    @GetMapping("verify/history/{id}/{startTime}/{endTime}")
    public Result verifyHistory(@PathVariable Long id, @PathVariable String startTime, @PathVariable String endTime) {
        try {
            return Result.okBuilder().data(stockStrategyService.verifyHistory(id, startTime, endTime)).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error();
    }
}
